import hashlib
import json
import numbers
import random
import time
import uuid
from datetime import datetime
import functools
import re

def isNumber(var):
    return isinstance(var, numbers.Number)

def isString(var):
    return isinstance(var, str)

def isDict(var):
    return isinstance(var, dict)

def isArray(var):
    return isinstance(var, list)


def get_classes_as_tuple(module):
    """获取模块中的所有类，封装成一个元组"""
    all_attributes = dir(module)
    class_objects = [getattr(module, attribute) for attribute in all_attributes if isinstance(getattr(module, attribute), type)]
    classes_tuple = tuple(class_objects)
    return classes_tuple

def hash_password(password):
        hashed_password = hashlib.md5(password.encode("utf-8"))
        return hashed_password.hexdigest()

def verify_password(password, hashed_password):
    return hash_password(password) == hashed_password

def dl_to_json(data: dict | list) -> str:
    """字典转json"""
    return json.dumps(data, ensure_ascii=False)

def json_to_dl(data: str) -> dict | list:
    """json转字典"""
    return json.loads(data)

def get_uuid():
    """获取uuid字符串"""
    return str(uuid.uuid4())

def get_now(format_string="%Y-%m-%d %H:%M:%S"):
    """获取当前时间 默认格式%Y-%m-%d %H:%M:%S"""
    current_time = datetime.now()
    return current_time.strftime(format_string)

def get_timestamp(time=None, format_string="%Y-%m-%d %H:%M:%S"):
    """获取对应时间格式的时间戳，传入参数为time和format_string，无参数传入时默认当前时间时间戳"""
    if time is None:
        time = get_now(format_string)
    timestamp = datetime.strptime(time, format_string).timestamp()
    return int(timestamp)

class _MethodDecoratorAdaptor:
    def __init__(self, decorator, func):
        self.decorator = decorator
        self.func = func
    def __call__(self, *args, **kwargs):
        return self.decorator(self.func)(*args, **kwargs)
    def __get__(self, instance, owner):
        return self.decorator(self.func.__get__(instance, owner))
    
def auto_adapt_to_methods(decorator):
    def adapt(func):
        return functools.wraps(func)(_MethodDecoratorAdaptor(decorator, func))
    return adapt

def is_valid_account(account):
    # 账号为5-26字符的数字或字母
    return re.match(r'^[a-zA-Z0-9]{5,26}$', account) is not None

def is_valid_password(password):
    # 密码为6-30字符的数字和密码，大小写字母，数字至少出现两种
    if not re.match(r'^[a-zA-Z0-9]{6,30}$', password):
        return False
    return len(set(password)) >= 2 and any(char.isdigit() for char in password)

def wait(timeout=None):
    time.sleep(timeout or get_random(2, 3))
    
def get_random(min_, max_, n=1) -> int|list:
    if n == 1:
        if min_ == max_: return min_
        return random.randint(min_, max_)
    if min_ == max_: return [ min_ for _ in range(n) ]
    return [ random.randint(min_, max_) for _ in range(n) ]

if __name__ == "__main__":
    print(dl_to_json(get_random(1, 5, 90)))
    pass